package com.warrior.gatherclient.service.impl;

import com.warrior.gatherclient.grpc.Messages;
import com.warrior.gatherclient.service.NetworkIFService;
import com.warrior.gatherclient.util.CommonUtil;
import com.warrior.gatherclient.service.SystemInfoService;
import com.warrior.gathercommon.constants.CommonStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import oshi.SystemInfo;
import oshi.hardware.NetworkIF;

import java.util.List;

/**
 * @ClassName NetworkIFServiceImpl.java
 * @Author xujian
 * @Description
 * @CreateTime 2020年12月16日 14:38:00
 */
@Service
@Slf4j
public class NetworkIFServiceImpl implements NetworkIFService {

    @Autowired
    private SystemInfoService systemInfoService;

    @Override
    public String getServiceKey() {
        return CommonStatus.CONSTANT_NETWORK_IF;
    }

    /**
     * @Author xujian
     * @Description 获取网卡信息
     * @Date 15:37 2020/12/16
     * @Param [systemInfo]
     **/
    private void addNetworkInfo(Messages.Builder messages, SystemInfo systemInfo) {
        List<NetworkIF> networkIFs = systemInfo.getHardware().getNetworkIFs();
        StringBuffer sb = new StringBuffer();
        for (NetworkIF networkIF : networkIFs) {
            String name = networkIF.getName();
            int mtu = networkIF.getMTU();
            sb.append(name).append(":").append(mtu).append(" | ");
        }
        Messages.Metric.Builder builder = Messages.Metric.newBuilder();
        builder.setMetricName(CommonStatus.CONSTANT_NETWORK_IF_INFO);
        builder.setMetricVaule(sb.toString());
        messages.addMetrics(builder);
    }

    private void addNetworkTraffic(Messages.Builder messages, SystemInfo systemInfo) {
        List<NetworkIF> networkIFs = systemInfo.getHardware().getNetworkIFs();
        for (NetworkIF networkIF : networkIFs) {
            String displayName = networkIF.getDisplayName();
            String recv = CommonUtil.formatByte(networkIF.getBytesRecv());
            String sent = CommonUtil.formatByte(networkIF.getBytesSent());
            Messages.Metric.Builder builder = Messages.Metric.newBuilder();
            builder.setMetricName(CommonStatus.CONSTANT_NETWORK_IF_BYTESSENT);
            builder.setMetricVaule(displayName + CommonStatus.CONSTANT_JOINTSTR + sent);
            messages.addMetrics(builder);

            Messages.Metric.Builder recvBuilder = Messages.Metric.newBuilder();
            recvBuilder.setMetricName(CommonStatus.CONSTANT_NETWORK_IF_BYTESRECV);
            recvBuilder.setMetricVaule(displayName + CommonStatus.CONSTANT_JOINTSTR + recv);
            messages.addMetrics(recvBuilder);
        }
    }

    @Override
    public Messages.Builder execute() {
        Messages.Builder messages = CommonUtil.initMessage(CommonStatus.CONSTANT_NETWORK_IF);
        SystemInfo systemInfo = systemInfoService.getSystemInfo();
        addNetworkInfo(messages, systemInfo);
        addNetworkTraffic(messages, systemInfo);

        return messages;
    }
}
